package com.xwaf.platform.system.provider.serviceimpl;

import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.xwaf.platform.common.api.entity.QuartzEntity;
import com.xwaf.platform.system.api.service.JobService;
import com.xwaf.platform.system.provider.dao.ELProjectDao;

@Service(value = "jobService")
public class JobServiceImpl implements JobService {

	@Autowired
	private Scheduler scheduler;

	@Autowired
	private ELProjectDao projeoctDao;

	@Override
	public void pause(QuartzEntity quartz) {
		try {
			JobKey key = new JobKey(quartz.getJobName(), quartz.getJobGroup());
			scheduler.pauseJob(key);
		} catch (SchedulerException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void resume(QuartzEntity quartz) {
		try {
			JobKey key = new JobKey(quartz.getJobName(), quartz.getJobGroup());
			scheduler.resumeJob(key);
		} catch (SchedulerException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void remove(QuartzEntity quartz) {
		try {
			TriggerKey triggerKey = TriggerKey.triggerKey(quartz.getJobName(), quartz.getJobGroup());
			// 停止触发器
			scheduler.pauseTrigger(triggerKey);
			// 移除触发器
			scheduler.unscheduleJob(triggerKey);
			// 删除任务
			scheduler.deleteJob(JobKey.jobKey(quartz.getJobName(), quartz.getJobGroup()));
			System.out.println("removeJob:" + JobKey.jobKey(quartz.getJobName()));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void add(QuartzEntity quartz) {
		try {
			// 如果是修改 展示旧的 任务
			if (quartz.getOldJobGroup() != null) {
				JobKey key = new JobKey(quartz.getOldJobName(), quartz.getOldJobGroup());
				scheduler.deleteJob(key);
			}
			Class cls = Class.forName(quartz.getJobClassName());
			cls.newInstance();
			// 构建job信息
			JobDetail job = JobBuilder.newJob(cls).withIdentity(quartz.getJobName(), quartz.getJobGroup())
					.withDescription(quartz.getDescription()).build();
			// 触发时间点
			CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(quartz.getCronExpression());
			Trigger trigger = TriggerBuilder.newTrigger()
					.withIdentity("trigger" + quartz.getJobName(), quartz.getJobGroup()).startNow()
					.withSchedule(cronScheduleBuilder).build();
			// 交由Scheduler安排触发
			scheduler.scheduleJob(job, trigger);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public Page<QuartzEntity> selPageList(Page<QuartzEntity> page, QuartzEntity quartz) {
		StringBuffer nativeSql = new StringBuffer();
		nativeSql.append(
				"SELECT job.JOB_NAME as jobName,job.JOB_GROUP as jobGroup,job.DESCRIPTION as description,job.JOB_CLASS_NAME as jobClassName,");
		nativeSql.append(
				"cron.CRON_EXPRESSION as cronExpression,tri.TRIGGER_NAME as triggerName,tri.TRIGGER_STATE as triggerState,");
		nativeSql.append("job.JOB_NAME as oldJobName,job.JOB_GROUP as oldJobGroup ");
		nativeSql.append("FROM qrtz_job_details AS job LEFT JOIN qrtz_triggers AS tri ON job.JOB_NAME = tri.JOB_NAME ");
		nativeSql.append("LEFT JOIN qrtz_cron_triggers AS cron ON cron.TRIGGER_NAME = tri.TRIGGER_NAME ");
		nativeSql.append("WHERE tri.TRIGGER_TYPE = 'CRON'");
		if (!StringUtils.isEmpty(quartz.getJobName())) {// 加入JobName搜索其他条件自行实现
			nativeSql.append(" AND job.JOB_NAME = " + quartz.getJobName());
		}
		EntityWrapper<QuartzEntity> wrapper = new EntityWrapper<>();
		wrapper.setSqlSelect(nativeSql.toString());
		return null;
	}
}
